<template>
  <div>
  <div class="mod-config"  v-if="visible">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="资产名" clearable></el-input>
      </el-form-item>

      <template>
        <el-select v-model="dataForm.key2" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.key2"
            :label="item.label"
            :value="item.key2">
          </el-option>
        </el-select>
      </template>


      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button  type="success" plain @click="exortData()">批量导出Excel</el-button>
        <el-button v-if="isAuth('assets:info:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('assets:info:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>

    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        width="50"
        label="序号" fixed="left">
      </el-table-column>
<!--      <el-table-column
        prop="assetsId"
        header-align="center"
        align="center"
        label="序号">
      </el-table-column>-->
      <el-table-column
        prop="assetsImg"
        header-align="center"
        align="center"
        label="资产照片">
        <template slot-scope="scope">
          <img :src=scope.row.assetsImg  style="width: 100px;height: 100px;">
        </template>
      </el-table-column>
      <el-table-column
        prop="assetsName"
        header-align="center"
        align="center"
        label="资产名称">
      </el-table-column>
      <el-table-column
        prop="assetsNum"
        header-align="center"
        align="center"
        label="资产编号">
      </el-table-column>
      <el-table-column
        prop="assetsBuytime"
        header-align="center"
        align="center"
        label="购入日期">
      </el-table-column>
      <el-table-column
        prop="assetsAdmin"
        header-align="center"
        align="center"
        label="使用人">
      </el-table-column>


      <el-table-column
        prop="assetsStatus"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.assetsStatus == 0">待用</el-tag>
          <el-tag              v-if="scope.row.assetsStatus == 1">审核中/领用</el-tag>
          <el-tag type="success" v-if="scope.row.assetsStatus == 2" >使用中</el-tag>
          <el-tag type="warning" v-if="scope.row.assetsStatus == 3" >维修中</el-tag>
          <el-tag type="danger" v-if="scope.row.assetsStatus == 4" >报废</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="assetsDesc"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="small" @click="assetsDetails(scope.row)">详情</el-button>
          <el-button v-if="isAuth('assets:info:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.assetsId)">修改</el-button>
          <el-button v-if="isAuth('assets:info:delete')" type="text" size="small" @click="deleteHandle(scope.row.assetsId)" :disabled="scope.row.assetsStatus != 0">删除</el-button>
          <el-button v-if="isAuth('assets:receive:applySave')" type="text"  size="small" @click="addapply(scope.row.assetsId)" :disabled="scope.row.assetsStatus!=0">申请领用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!--    领用申请-->
    <info-dialog v-if="applyVisible" ref="apply" @refreshDataList="getDataList"></info-dialog>
    <!--    详情-->
    <assets-details v-if="assetsDetailsVisible" ref="assetsDetails" @goBack="goback()"></assets-details>
  </div>
</template>

<script>
  import AddOrUpdate from './info-add-or-update'
  import infoDialog from './infoDialog'
  import assetsDetails from './assetsDetails'
  export default {
    data () {
      return {
        options: [{
          key2: '',
          label: '全部'
        }, {
          key2: '0',
          label: '待用'
        }, {
          key2: '1',
          label: '领用申请中'
        }, {
          key2: '2',
          label: '使用中'
        }, {
          key2: '3',
          label: '维修中'
        }, {
          key2: '4',
          label: '报废'
        }],
        applyVisible: false,
        dataForm: {
          key: '',
          key2: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],                 // 勾选的
        addOrUpdateVisible: false,
        assetsDetailsVisible: false,
        visible: true
      }
    },
    components: {
      AddOrUpdate,
      infoDialog,
      assetsDetails
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // Excel导出
      exortData () {
        const idStr = this.dataListSelections.map(item => `${item.assetsId}`).join(',')
        /* const u = `http://localhost:8080/excel/export?token=${this.$cookie.get('token')}&ids=${idStr}` */
        /* console.log('数据  ' + idStr) */
        if (!this.dataListSelections.length) { // 没有勾选默认全部导出
          window.open(`http://localhost:8080/excel/export?token=${this.$cookie.get('token')}&key2=${this.dataForm.key2}&assetsName=${this.dataForm.key}`)
        } else { // [1,2,3]→ "1","2","3"
          window.open(`http://localhost:8080/excel/export?token=${this.$cookie.get('token')}&ids=${idStr}`)
        }
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/assets/info/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'key2': this.dataForm.key2
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 领用 加入领用表
      addapply (id) {
        const selectedRow = this.dataList.find(row => row.assetsId === id)   // 点击领用拿到这一行的ID
        // console.log('一行' + selectedRow.createStatus)
        if (selectedRow && selectedRow.assetsStatus !== '0') {
          this.$message({           // 如果审核过了 this.applyVisible = false，其他才进入审核页面 this.$refs.apply.init(id)
            message: '该资产已经被领用了',
            onClose: () => {
              this.applyVisible = false
            },
            type: 'warning'
          })
        } else {
          this.applyVisible = true
          this.$nextTick(() => {
            this.$refs.apply.init(id)
          })
        }
      },
      // 详情
      assetsDetails (row) {
        this.visible = false
        this.assetsDetailsVisible = true
        this.$nextTick(() => {
          this.$refs.assetsDetails.init(row)
        })
      },
      // 详情组件跳转回父组件
      goback () {
        this.assetsDetailsVisible = false
        this.visible = true
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },

      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.assetsId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/assets/info/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
